<template>
  <view class="uni-app">
    <view class="status-bar" />
    <view class="main-container">
      <view class="logo-box">
        <image :src="$static('images/logo.png')" alt="" class="logo" />
      </view>
      <view class="app-name">
        {{ appName }}
      </view>
      <view class="subhead">
        实现企业『销售+管理』全流程
      </view>

      <login-form v-model="form" @formSubmit="loginByUsername" />
      <!--企业微信登录小程序端-->
      <view v-if="isWeixin" class="bottom-side-otherLogin" @click="getWeChatCode">
        <text>企业微信内部登录</text>
        <!--<image src="https://xuezhifu-resource.oss-cn-hangzhou.aliyuncs.com/newxuefu/mwx/wx.png"></image>-->
      </view>

      <view class="law">
        <text class="wk wk-check-fill icon" :class="{active: agreeLaw}" />
        我已阅读并同意
        <text class="special" @click.stop="handleTo('/pages_law/service')">
          《用户协议》
        </text>
        与
        <text class="special" @click.stop="handleTo('/pages_law/privacy')">
          《隐私声明》
        </text>
      </view>
    </view>
  </view>
</template>

<script>
  import LoginForm from './components/loginForm.vue'
  import {providerLogin, renderWechatAPI, getCodeImg, login, getUserInfo } from 'API/login'
  import { Encrypt } from '@/utils/crypto'; // 导入加密函数

  import {
    appName,
    appVersion
  } from '@/config'
  // 2024/8/1 @description:增加引入mapGetters
  import {
    mapActions, mapGetters
  } from 'vuex';
  import { corpid, redirectUri } from 'config' // '../config.js'
  import WkDragButton from '../../components/wk-drag-button/wk-drag-button';

  export default {
    name: 'LoginIndex',
    components: {
		
      LoginForm
    },
    data() {
      return {
        appName: appName,
        form: {
          username: '',
          password: '',
          cpCode: '',
          loginType: 1,
          imgUuid: undefined, // redis的key
          code: '', // 验证码
        },
        codeUrl: '', // 验证码图片，请求发送后把map集合的img解析到这里
        agreeLaw: true,
        isWeixin: false,
        code: ''
      }
    },
    computed: {
    },
    onLoad() {
      this.$clearStorage()
      const username = uni.getStorageSync('username') || ''
      this.$set(this.form, 'username', username)
    },
    onShow() {
      this.$clearStorage()
    },
    methods: {
      /**
       * 账号登陆
       */
      loginByUsername(form, errFunc) {
        // const mobileReg = /1[1-9]\d{9}$/
        if (!form.username || form.username === '') {
          this.$toast('登录账号不能为空')
          return
        }
        if (!form.password || form.password === '') {
          this.$toast('密码不能为空')
          return
        }
        if (!form.code || form.code === '') {
          this.$toast('请输入验证码')
          return
        }
        try {
          // 在发送请求之前加密密码
          const encryptionKey = '_aes_secret_key_';
          const ivVector = '_aes_secret_iv__';
          form.password = Encrypt(form.password, encryptionKey, ivVector);
          login(form).then(res => {
            console.log(res)
            if (res.code == 0) {
              // 如果token存在就是登录成功了
              console.log('登录成功')
              // 记录用户名
              // uni.setStorageSync('username', data.username);          
              uni.setStorageSync('token', res.data); // 提取 token 部分
              uni.reLaunch({
                url: '/pages/home/index'
              })
            } else {
              errFunc()
            }
          }).catch(() => {
          })        
          // 处理登录成功的逻辑
        } catch (error) {
          console.error('登录失败:', error);
          // 处理登录失败的逻辑
        }
      }
    }
  }
</script>

<style scoped lang="scss">
	@import "./style/style.scss";

  .placehoder {
		font-size: 30rpx;
	}

	.status-bar {
		background: white;
	}

	.main-container {
		width: 100%;
		height: 100%;
		padding: 0 40rpx;
		font-size: 32rpx;
		background-color: white;

		.logo-box {
			margin: 66rpx auto 0;
		}
	}
</style>
